package com.basic.library.utils.audio;

import android.content.Context;
import android.media.AudioManager;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.basic.library.utils.MyLog;

/**
 * @author yj
 * @description 焦点问题
 */
public class MAudioManager {
    private static MAudioManager mAudioManager = new MAudioManager();
    private AudioManager am;

    public static MAudioManager getInstance() {
        return mAudioManager;
    }


    public AudioManager getAm(Context context) {
        if (am == null) {
            am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        }
        return am;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public String getText() {
        String text = "";
        text += "通话 AudioManager.STREAM_VOICE_CALL ：" + am.isStreamMute(AudioManager.STREAM_VOICE_CALL) + "\n";
        text += "系统声音 AudioManager.STREAM_SYSTEM ：" + am.isStreamMute(AudioManager.STREAM_SYSTEM) + "\n";
        text += "铃声 AudioManager.STREAM_RING ：" + am.isStreamMute(AudioManager.STREAM_RING) + "\n";
        text += "音乐 AudioManager.STREAM_MUSIC ：" + am.isStreamMute(AudioManager.STREAM_MUSIC) + "\n";
        text += "铃声 AudioManager.STREAM_ALARM ：" + am.isStreamMute(AudioManager.STREAM_ALARM) + "\n";
        text += "通知音 AudioManager.STREAM_NOTIFICATION ：" + am.isStreamMute(AudioManager.STREAM_NOTIFICATION) + "\n";
        text += "是否有音乐处于活跃状态 ：" + am.isMusicActive() + "\n";
        text += "是否打开扬声器 ：" + am.isSpeakerphoneOn() + "\n";
        text += "音频输出是否通过蓝牙耳机 ：" + am.isBluetoothA2dpOn() + "\n";
        return text;
    }

    public void initAudioManager(Context context) {
        if (am == null)
            am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        MAudioManager.getInstance().muteAudioFocus(true);
    }

    /**
     * 是否短暂的获取焦点
     *
     * @param bMute
     * @return
     */
    public boolean muteAudioFocus(boolean bMute) {
        boolean bool = false;
        if (am != null)
            //关闭声音
            if (bMute) {
//                MAudioManager.getInstance().setHalfVolume();
                int result = am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
                bool = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
            } else {
                //恢复声音
                int result = am.abandonAudioFocus(null);
                bool = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
            }
        MyLog.p("pauseMusic bMute=" + bMute + " result=" + bool);
        return bool;
    }

    AudioManager.OnAudioFocusChangeListener listener = new AudioManager.OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusChange) {
            switch (focusChange) {
                case AudioManager.AUDIOFOCUS_LOSS:
                    //长时间丢失焦点,当其他应用申请的焦点为AUDIOFOCUS_GAIN时，
                    //会触发此回调事件，例如播放QQ音乐，网易云音乐等
                    //通常需要暂停音乐播放，若没有暂停播放就会出现和其他音乐同时输出声音
//                        release();
                    //释放焦点，该方法可根据需要来决定是否调用
                    //若焦点释放掉之后，将不会再自动获得
                    muteAudioFocus(false);
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    //短暂性丢失焦点，当其他应用申请AUDIOFOCUS_GAIN_TRANSIENT或AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE时，
                    //会触发此回调事件，例如播放短视频，拨打电话等。
                    //通常需要暂停音乐播放
//                        pause();
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    //短暂性丢失焦点并作降音处理
                    break;
                case AudioManager.AUDIOFOCUS_GAIN:
                    //当其他应用申请焦点之后又释放焦点会触发此回调
                    //可重新播放音乐
//                        resume();
                    break;
            }
        }
    };


    public void setHalfVolume() {
        //最大音量
        int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        //当前音量
        int currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
        am.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume / 2 + 1, 0);
    }

}
